//
//  SearchViewController.m
//  BGM
//
//  Created by liuqin on 2018/11/26.
//  Copyright © 2018年 com.alirtc.demo. All rights reserved.
//

#import "SearchViewController.h"
#import "Common.h"
#import "BGMHTTP.h"
#import "BGMUser.h"

#import "SearHomeTableViewCell.h"
#import "SuyouSearchTableViewCell.h"
#import "JFCityViewController.h"
#import "SVProgressHUD.h"

@interface SearchViewController ()<UITableViewDataSource,UITableViewDelegate,
SuyouSearchTableViewCellDelegate,
SearHomeTableViewCellDelegate>

@property (nonatomic, strong)UITableView *searchTable;

@property (nonatomic, copy)NSString *searchUserType; //类型
@property (nonatomic, copy)NSString *searchPublicType; //类型

@property (nonatomic, copy)NSString *searchCity; //城市
@property (nonatomic, copy)NSString *searchSex; //性别
@property (nonatomic, copy)NSString *searchPay; //有偿




@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navview.hidden = NO;
    self.titleBtn.hidden = NO;
    [self.titleBtn setTitle:@"筛选" forState:UIControlStateNormal];
    self.navRightBtn.hidden = NO;
    [self.navRightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [self.navRightBtn setTitleColor:bgmmainColor forState:UIControlStateNormal];
    [self getUserSearchHistory];
 
    
    self.searchTable = [[UITableView alloc]initWithFrame:CGRectMake(0, self.navview.frame.size.height, SCREENW, SCREENH-self.navview.frame.size.height)];
    self.searchTable.delegate = self;
    self.searchTable.dataSource = self;
    self.searchTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.searchTable.backgroundColor = bgmColor(244, 244, 244);
    self.searchTable.scrollEnabled = NO;
    [self.view addSubview:self.searchTable];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(searchSeleCity:) name:@"searchSeleCity" object:nil];
    

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 145;
    }else if (indexPath.row ==1){
        return 105;
    }
    return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < 2) {
        static NSString *idenfier = @"SuyouSearchTableViewCell";
        SuyouSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idenfier];
        if (cell == nil) {
            cell = [[SuyouSearchTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idenfier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.pushDelegate = self;
        }
        
        if (indexPath.row ==0) {
            NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
            NSArray *array = [ud objectForKey:@"PUSHTYPE"];
            [cell setTieleAndTxt:@"素友帮" array:array type:@"1" isPublic:self.isPublic seleStr:self.searchUserType];
            
        }else{
            [cell setTieleAndTxt:@"素公益" array:@[] type:@"2" isPublic:self.isPublic seleStr:self.searchPublicType];
        }
        
        return cell;
    }
    else{
        
        static NSString *identifier = @"SearHomeTableViewCell";
        SearHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[SearHomeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.searHomeDelegate = self;
            
        }
        
        if (indexPath.row == 2) {
            [cell searchHomeTitle:@"城市" rightText:self.searchCity btn:NO swith:YES isPublic:self.isPublic seleStr:self.searchCity];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }else if (indexPath.row == 3){
            [cell searchHomeTitle:@"性别" rightText:@"" btn:YES swith:YES isPublic:self.isPublic seleStr:self.searchSex];
            cell.accessoryType = UITableViewCellAccessoryNone;
            
        }else if (indexPath.row == 4){
            cell.accessoryType = UITableViewCellAccessoryNone;
            [cell searchHomeTitle:@"有偿" rightText:@""btn:NO swith:NO isPublic:self.isPublic seleStr:self.searchPay];
        }
        
        return cell;
    
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row ==2 && [self.isPublic isEqualToString:@"0"]) {
        JFCityViewController *vc = [[JFCityViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)getUserSearchHistory{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [userDefaults objectForKey:@"getUserSearchHistory"];
    if ([dic objectForKey:@"searchCity"]) {
        self.searchUserType = [dic objectForKey:@"searchUserType"];
        self.searchPublicType = [dic objectForKey:@"searchPublicType"];
        self.searchPay = [dic objectForKey:@"searchPay"];
        self.searchSex = [dic objectForKey:@"searchSex"];
        self.searchCity = [dic objectForKey:@"searchCity"];
        [self.searchTable reloadData];
    }else{
        self.searchUserType = @"";
        self.searchPublicType = @"";
        self.searchPay = @"";
        self.searchSex = @"";
        self.searchCity = @"";
        [self.searchTable reloadData];
    }
    
}

- (void)saveSarechHistory{
    
    
    if (self.searchPay.length==0) {
        self.searchPay = @"2";
    }
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = @{@"searchUserType":self.searchUserType.length>0?self.searchUserType:@"",@"searchPublicType":self.searchPublicType.length>0?self.searchPublicType:@"",@"searchCity":self.searchCity,@"searchSex":self.searchSex,@"searchPay":self.searchPay};
    [userDefaults setObject:dic forKey:@"getUserSearchHistory"];
    [userDefaults synchronize];
    
}


- (void)searchPublicPushtype:(NSArray *)array{
   
    if (array.count==0) {
        self.searchPublicType = @"";
    }else{
        NSMutableString *str = [[NSMutableString alloc]init];
        for (NSString *tag in array) {
            [str appendString:[NSString stringWithFormat:@"%@,",tag]];
        }
        [str deleteCharactersInRange:NSMakeRange(str.length-1, 1)];
        self.searchPublicType = str;
    }

}
- (void)searchUserPushtype:(NSArray *)array{
    if (array.count >0) {
        NSMutableString *str = [[NSMutableString alloc]init];
        for (NSString *tag in array) {
            [str appendString:[NSString stringWithFormat:@"%@,",tag]];
        }
        [str deleteCharactersInRange:NSMakeRange(str.length-1, 1)];
        self.searchUserType = str;
    }else{
        self.searchUserType = @"";
    }
    
   

}
-(void)clickSex:(NSString *)sex{
    if ([sex isEqualToString:@"3"]) {
        self.searchSex = @"";
    }else{
        self.searchSex = sex;
    }
}
-(void)clickPay:(NSString *)pay{
    self.searchPay = pay;
}
- (void)searchSeleCity:(NSNotification *)noti{
    NSString *city = noti.object;
    self.searchCity = city;
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:2 inSection:0];
    [self.searchTable reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
     
     
}

#pragma mark 筛选
- (void)clickRight{
    
    [SVProgressHUD showWithStatus:@"正在查找"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [self saveSarechHistory];
    
    
    NSString *type = @"";
    NSString *Sex = @"";
    NSString *itisPaid = @"";

    if ([self.isPublic isEqualToString:@"0"]) {
        type = self.searchUserType;
        Sex = self.searchSex;
        itisPaid = self.searchPay;

    }else{
        type = self.searchPublicType;
        Sex = @"";
        itisPaid = @"";

    }
    //9 寻亲团圆 生命接力  爱心助学
    NSDictionary *param = @{@"usersId":[BGMUser shareInstance].userId,
                            @"userToken":[BGMUser shareInstance].userToken,
                            @"publishType":type,
                            @"city":@"",
                            @"gender":Sex,   //1男2女 空不限
                            @"itisPaid":itisPaid, //1有偿2无偿
                            @"pageNum":@"1",
                            @"pageSize":@"10"
                            };
    
    [BGMHTTP requestWithMethodName:@"index/serch" parameters:param completion:^(BOOL success, NSError *error, NSDictionary *result) {
        NSString *code = [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
        if ([code isEqualToString:@"200"]) {
            NSArray *array = [result objectForKey:@"data"];
            NSDictionary *dic = @{@"array":array,@"param":param};
            [[NSNotificationCenter defaultCenter]postNotificationName:@"UserSearchMsg" object:dic];
            [SVProgressHUD dismiss];
        }else{
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"类型【11】爱心助学 搜索错误 code= %@",code]];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    }];
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
